package com.ncr.travel.appmgr.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.ncr.travel.appmgr.client.ConfigurationItem;
import com.ncr.travel.appmgr.client.event.ConfirmConfigChangeEvent;
import com.ncr.travel.appmgr.client.event.PreviewConfigChangeEvent;
import com.ncr.travel.appmgr.client.event.UndoConfigChangeEvent;
import com.ncr.travel.appmgr.client.mvp.EventBus;
import com.ncr.travel.appmgr.client.ui.ConfirmationDialog;
import com.ncr.travel.appmgr.model.shared.ScreenConfiguration;

/**
 * @author ng185038
 * 
 */
public class ConfigEditorWidget extends Composite implements ConfigEditorView {

	private static ConfigEditorUiBinder uiBinder = GWT.create(ConfigEditorUiBinder.class);

	@UiTemplate("ConfigEditorView.ui.xml")
	interface ConfigEditorUiBinder extends UiBinder<Widget, ConfigEditorWidget> {
	}

	private ConfirmationDialog dialogBox;
	private ConfigurationItem configurationItem;

	@UiField
	Button undoBtn;

	@UiField
	Button previewBtn;

	@UiField
	Button confirmBtn;

	private EventBus eventBus;

	private ScreenConfiguration screenConfiguration;

	public ConfigEditorWidget(String confirmText, EventBus eventBus) {
		initWidget(uiBinder.createAndBindUi(this));
		System.out.println("Constructing ConfigEditorWidget");
		this.dialogBox = new ConfirmationDialog(confirmText, null);
		this.eventBus = eventBus;
		disableAllControls();
	}

	@Override
	public ConfirmationDialog getConfirmationDialog() {
		return dialogBox;
	}

	@Override
	public void setScreenConfiguration(ScreenConfiguration screenConfiguration) {
		this.screenConfiguration = screenConfiguration;
	}

	public void setConfigurationItem(final ConfigurationItem configurationItem) {
		this.configurationItem = configurationItem;
	}

	@UiHandler("undoBtn")
	void undoOnClick(ClickEvent event) {
		eventBus.fireEvent(new UndoConfigChangeEvent(configurationItem));
		disableAllControls();
	}

	@UiHandler("previewBtn")
	void previewOnClick(ClickEvent event) {
		eventBus.fireEvent(new PreviewConfigChangeEvent(configurationItem));
		disablePreviewControl();
		enableConfirmControl();
	}

	@UiHandler("confirmBtn")
	void confirmOnClick(ClickEvent event) {
		// For Text - confirmation dialog should not be shown
		if (ConfigurationItem.TEXT.equals(configurationItem)) {
			disableAllControls();
			eventBus.fireEvent(new ConfirmConfigChangeEvent(configurationItem, screenConfiguration));
		} else {
			dialogBox.showView();
			dialogBox.getYesBtn().addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					dialogBox.hide();
					dialogBox = null;
					disableAllControls();
					eventBus.fireEvent(new ConfirmConfigChangeEvent(configurationItem, screenConfiguration));
				}
			});
			dialogBox.getNoBtn().addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					disableConfirmControl();
					enablePreviewControl();
					dialogBox.hide();
					dialogBox = null;
				}
			});
		}
	}

	public void enableUndoControl() {
		undoBtn.setEnabled(true);
	}

	public void disableUndoControl() {
		undoBtn.setEnabled(false);
	}

	public void enablePreviewControl() {
		previewBtn.setEnabled(true);
	}

	public void disablePreviewControl() {
		previewBtn.setEnabled(false);
	}

	public void enableConfirmControl() {
		confirmBtn.setEnabled(true);
	}

	public void disableConfirmControl() {
		confirmBtn.setEnabled(false);
	}

	@Override
	public void disableAllControls() {
		disableUndoControl();
		disablePreviewControl();
		disableConfirmControl();
	}

}